import logging

import allure
import jsonpath

from untils.send_request import send_jdbc_request


@allure.step("核心3:http响应断言")
def http_assert(case,response):
    # 核心步骤三：http处理断言
    expected = eval(case["expected"])
    if case["check"]:
        # 转为列表，因为excel中读取出来的是"[]"
        check=eval(case["check"])
        # 将检验字段的列表与预期结果的列表使用zip函数，将其一一对应
        for c,e in zip(check,expected):
            res=jsonpath.jsonpath(response.json(), c)[0]
            logging.info("3.http响应断言内容：实际结果{} == 预期结果{}".format(res, e))
            assert res == e
    else:
        for e in expected:
            logging.info("3.http响应断言内容：预期结果{} in 响应结果{}".format(e, response.text))
            assert e in response.text


def jdbc_assert(case):
    # 核心步骤三：jdbc处理断言
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("核心3:jdbc响应断言"):
            sqlname=send_jdbc_request(case["sql_check"])
            logging.info("3.jdbc响应断言内容：实际结果{} == 预期结果{}".format(sqlname, case["sql_expected"]))
            assert sqlname == case["sql_expected"]